﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.Admin
{
    public partial class Link_Modify : BaseCode.BaseAdmin
    {
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string value = Request.QueryString["id"];
                id = 0;
                if (int.TryParse(value, out id) && id != 0)
                {
                    this.InitPageInfo();
                }
                else
                {
                    //返回到登录页面
                    Response.Redirect("/login.aspx");
                }
            }
        }

        private void InitPageInfo()
        {
            BLL.BLL_Link bll = new BLL.BLL_Link();
            Model.Model_Link model = bll.GetModel(id);
            this.page_name.Value = model.name;
            this.page_remark.Value = model.remark;
            this.page_topName.Value = model.title;
            this.page_url.Value = model.url;
            this.linkid.Value = this.id.ToString();
            //打开方式
            if (model.target == "_blank")
            {
                this.page_rdoOpenCur.Checked = true;
            }
            else if (model.target == "_parent")
            {
                this.page_rdoOpenNew.Checked = true;
            }
            //是否启用
            if (model.state == 0)
            {
                this.page_rdoClose.Checked = true;
            }
            else
            {
                this.page_rdoOpen.Checked = true;
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BLL.BLL_Link bll = new BLL.BLL_Link();
            Model.Model_Link model = new Model.Model_Link();
            model.id = int.Parse(this.linkid.Value);
            model.name = this.page_name.Value;
            model.title = this.page_topName.Value;
            model.url = this.page_url.Value;
            model.remark = this.page_remark.Value;
            model.updatetime = System.DateTime.Now;

            if (this.page_rdoOpenCur.Checked == true)
                model.target = "_blank";
            else
                model.target = "_parent";

            if (this.page_rdoOpen.Checked == true)
                model.state = 1;
            else
                model.state = 0;

            if (bll.Update(model) > 0)
            {
                Response.Redirect("Link.aspx");
            }
            else
            {
                Common.RunScript.Alert(this, "修改失败");
            }
        }
    }
}